* Create table from placebo shooting regressions
*
*    Requires command keeporder from ssc:
*        ssc install keeporder

set more off

include "bill-analysis/bill_setup.do"

loc pfx "z"  // which variables to check distribution of?


// label variables {{{
// ----------------------------------------------------------------------------
capture la drop dv
la def dv 1 "Bills" 2 "Laws" 3 "Tightening Laws" 4 "Loosening Laws" 5 "Total"

capture program drop lblplacebo
program define lblplacebo
    *! Create labels for grouping variables
    syntax varname [, GENerate(name)]
    if ("`generate'" == "") loc generate = "dv"

    replace `varlist' = "Bills" if `varlist' == "cntbill_yr"
    replace `varlist' = "Laws"  if `varlist' == "cntbill_laws_yr"
    replace `varlist' = "Tightening Laws" if `varlist' == "laws_tighten"
    replace `varlist' = "Loosening Laws"  if `varlist' == "laws_loosen"
    encode `varlist', gen(`generate')
end

capture program drop lblpolitics
program define lblpolitics
    replace pol = "Legislature" if pol == "leg"
    replace pol = "Trifecta" if pol == "tri"
    replace party = "Democrat" if party == "dem"
    replace party = "Republican" if party == "rep"
    replace party = "Split Control" if party == "spl"
end  // }}}


loc stats "p1 p5 p10 p90 p95 p99"

// distribution of placebo effect for main regressions {{{
// ----------------------------------------------------------------------------
use "`out'/placebo_main_effects.dta", clear
assert cv == 1  // check that all runs converged
gen dvmdl = dv + ":" + mdl

tabstat `pfx'shoot `pfx'fatal `pfx'avg `pfx'big, by(dvmdl) stats(`stats') save
qui tabstatmat M

clear
qui svmat2 M, names(col) rnames(ss) full
qui split ss, parse(";" ":") limit(3)
rename (ss1 ss2 ss3) (dvname mdl size)

// dummy only model has no "average" and "big" estimates
drop if mdl == "dummy" & inlist(size, "`pfx'fatal", "`pfx'avg", "`pfx'big")

lblplacebo dvname, gen(dv)
drop if dv == 5

keeporder dv mdl size p1-p99
format %9.2f p1-p99
export excel "`out'/placebo_tables.xlsx", firstrow(var) sheet("main") sheetmod
// }}}


// distribution of placebo effect for political regressions {{{
// ----------------------------------------------------------------------------
use "`out'/placebo_pol_effects.dta", clear
assert cv_dum == 1 & cv_fat == 1  // check that all models converged
gen dvpol = dv + ":" + pol

qui tabstat `pfx'dum_rep `pfx'dum_dem `pfx'dum_spl  /*
         */ `pfx'avg_rep `pfx'avg_dem `pfx'avg_spl  /*
         */ `pfx'big_rep `pfx'big_dem `pfx'big_spl  /*
    */ , by(dvpol) stats(`stats') save
tabstatmat M

clear
quietly {
    svmat2 M, names(col) rnames(ss) full
    split ss, parse(";" ":") limit(3)
    split ss3, parse("_") limit(2)
}
rename (ss1 ss2 ss31 ss32) (dvname pol size party)
drop ss*

lblplacebo dvname, gen(dv)
drop if dv == 5
lblpolitics

keeporder dv size pol party p1-p99
format %9.2f p1-p99
export excel "`out'/placebo_tables.xlsx", firstrow(var) sheet("pol") sheetmod
// }}}


// distribution of placebo effect with state-specific trends {{{
// ----------------------------------------------------------------------------
use "`out'/time-trend/placebo_main_effects.dta", clear
assert cv == 1  // check that all runs converged
gen dvmdl = dv + ":" + mdl

tabstat `pfx'shoot `pfx'fatal `pfx'avg `pfx'big, by(dvmdl) stats(`stats') save
qui tabstatmat M

clear
qui svmat2 M, names(col) rnames(ss) full
qui split ss, parse(";" ":") limit(3)
rename (ss1 ss2 ss3) (dvname mdl size)

// dummy only model has no "average" and "big" estimates
drop if mdl == "dummy" & inlist(size, "`pfx'fatal", "`pfx'avg", "`pfx'big")

lblplacebo dvname, gen(dv)
drop if dv == 5

keeporder dv mdl size p1-p99
format %9.2f p1-p99
export excel "`out'/placebo_tables.xlsx", firstrow(var) sheet("trends") sheetmod
// }}}

exit
// vim: foldmethod=marker
